package net.code.btalk.ui;

import net.code.btalk.SavedData;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.Keypad;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.CheckboxField;
import net.rim.device.api.ui.component.EditField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.container.MainScreen;

public class OptionScreen extends MainScreen {
	CheckboxField ringCurCheckbox;
	CheckboxField vibCurCheckbox;
	CheckboxField ringOtherCheckbox;
	CheckboxField vibOtherCheckbox;
	CheckboxField ledOtherCheckbox;
	CheckboxField popupCheckbox;
	
	CheckboxField autoRetryCheckbox;
	EditField delayEditField;
	EditField retryLimEditField;
	
	EditField fontSizeEditField;
	
	public OptionScreen() {
		this.setTitle("Options");
		
		
		ringCurCheckbox = new CheckboxField("Sound(Current)", SavedData.ringForCurrent);
		vibCurCheckbox = new CheckboxField("Vibration(Current)", SavedData.vibForCurrent);
		
		ringOtherCheckbox = new CheckboxField("Sound(Other)", SavedData.ringForOther);
		vibOtherCheckbox = new CheckboxField("Vibration(Other)", SavedData.vibForOther);
		ledOtherCheckbox = new CheckboxField("Led blink on unread", SavedData.ledForOther);
		popupCheckbox = new CheckboxField("Popup on new message", SavedData.popupOnMsg);
		
		autoRetryCheckbox = new CheckboxField("Auto reconnect", SavedData.autoRetry);
		delayEditField = new EditField("Retry delay(sec): ", String.valueOf(SavedData.retryDelay), 10, EditField.FILTER_NUMERIC);
		retryLimEditField = new EditField("Retry limit: ", String.valueOf(SavedData.retryLimit), 10, EditField.FILTER_NUMERIC);
		
		fontSizeEditField = new EditField("Font size: ", String.valueOf(SavedData.fontSize), 10, EditField.FILTER_NUMERIC);		
		
		this.add(new LabelField("Notification"));
		this.add(ringCurCheckbox);
		this.add(vibCurCheckbox);
		
		this.add(ringOtherCheckbox);
		this.add(vibOtherCheckbox);
		this.add(ledOtherCheckbox);
		this.add(popupCheckbox);
		
		this.add(new SeparatorField());
		this.add(new LabelField("Auto reconnection"));
		this.add(autoRetryCheckbox);
		this.add(delayEditField);
		this.add(retryLimEditField);
		
		this.add(new SeparatorField());
		this.add(new LabelField("Appearance"));
		this.add(fontSizeEditField);
		
		this.add(new ButtonField("Save") {
			protected boolean keyChar(char key, int status, int time) {
				if (key == Keypad.KEY_ENTER) {
					setOptions();					
					SavedData.saveOptions();
					this.getScreen().getUiEngine().popScreen(this.getScreen());
					return true;
				} else {
					return false;
				}
			}
		});
		
		this.add(new ButtonField("Cancel") {
			protected boolean keyChar(char key, int status, int time) {
				if (key == Keypad.KEY_ENTER) {
					this.getScreen().getUiEngine().popScreen(this.getScreen());
					return true;
				} else {
					return false;
				}
			}
		});
	}
	
	protected boolean onSave() {
		setOptions();
		
		SavedData.saveOptions();
		return true;
	}
	
	private void setOptions() {
		SavedData.ringForCurrent = ringCurCheckbox.getChecked();
		SavedData.vibForCurrent = vibCurCheckbox.getChecked();
		
		SavedData.ringForOther = ringOtherCheckbox.getChecked();
		SavedData.vibForOther = vibOtherCheckbox.getChecked();
		SavedData.ledForOther = ledOtherCheckbox.getChecked();
		SavedData.popupOnMsg = popupCheckbox.getChecked();
		
		SavedData.autoRetry = autoRetryCheckbox.getChecked();
		SavedData.retryDelay = Integer.parseInt(delayEditField.getText());
		SavedData.retryLimit = Integer.parseInt(retryLimEditField.getText());
		SavedData.fontSize = Integer.parseInt(fontSizeEditField.getText());
	}
}
